﻿using System;
using System.Collections.Generic;
using ZyGames.DZPocker.Model;
using ZyGames.Framework.Cache.Generic;
using ZyGames.Framework.Game.Cache;
using ZyGames.Framework.Game.Context;
using ZyGames.Framework.Game.Contract;
using ZyGames.Framework.Game.Contract.Action;
using ZyGames.Framework.Game.Lang;
using ZyGames.Framework.Game.Service;
using System.Linq;
using ZyGames.DZPocker.Bll.Logic;
using ZyGames.DZPocker.Script;
using ZyGames.Framework.Net;
using ZyGames.Framework.Common.Serialization;
using System.Threading;
using ZyGames.DZPocker.Tools;

namespace ZyGames.DZPocker.Script.CsScript.Action
{
	/// <summary>
	/// 请求桌子成绩
	/// </summary>
	public class Action30037 : BaseAction
	{
		private int tableID;

		private string tablesStr;
		private bool result = false;

		public Action30037(HttpGet actionGetter)
			: base((short)ActionType.DZApplyBattleTableScore, actionGetter)
		{
		}
		public override bool GetUrlElement()
		{
			if (httpGet.GetInt("TableID", ref tableID))
			{
				return true;
			}
			return false;
		}

		public override bool TakeAction()
		{
			var gameTable = GameTable.Current.GetGameTableData(tableID);
			if (gameTable == null)
			{
				Tool.WriteErrorLine("没有当前牌桌记录");
				return false;
			}
			var profitRecords = new List<ProfitRecord>();
			var tableProfitRecord = new TableProfitRecord();
			tableProfitRecord.BeginTime = gameTable.StartTime;
			tableProfitRecord.smallBind = gameTable.SmallBind;
			tableProfitRecord.bigBind = gameTable.BigBind;
			tableProfitRecord.TableMaster = new SimpleUserData() { NickName = gameTable.MasterNickName, HeadIcon = gameTable.MasterHeadIcon, UserId = gameTable.MasterUserId };
			//var userTables = GameTable.Current.GetUserTablesByTableId(tableID);
			//if (userTables.Count <= 0)
			//{
			//	Tool.WriteErrorLine("没有玩家记录");
			//	return false;
			//}
			//int MaxLoseChip = 0;
			//int MaxWinChip = 0;
			//int MaxApplyChip = 0;
			//int MaxLoseUserId = 0;
			//int MaxWinUserId = 0;
			//int MaxApplyUserId = 0;
			//foreach (var userTable in userTables)
			//{
			//	if(userTable.Profit > MaxWinChip)
			//	{
			//		MaxWinChip = userTable.Profit;
			//		MaxWinUserId = userTable.UserID;
			//	}
			//	if(userTable.Profit < MaxLoseChip)
			//	{
			//		MaxLoseChip = userTable.Profit;
			//		MaxLoseUserId = userTable.UserID;
			//	}

			//	//TODO:还有申请的筹码
			//	profitRecords.Add(new ProfitRecord(){ BuyChip = 200, Profit = userTable.Profit, User = new SimpleUserData(){ UserId = userTable.UserID, HeadIcon = userTable.HeadIcon, NickName = userTable.NickName}});
			//}
			//tableProfitRecord.ProfitRecords = profitRecords;
			//var mvpUser = GameTable.Current.GetUser(MaxWinUserId);
			//if (mvpUser == null)
			//{
			//	Tool.WriteErrorLine("消息30037MVP不存在");
			//	return false;
			//}
			//var mvp = new SimpleUserData() { NickName = mvpUser.GetNickName(), HeadIcon = mvpUser.HeadIcon, UserId = mvpUser.UserId };
			//var bigFishUser = GameTable.Current.GetUser(MaxLoseUserId);
			//if (bigFishUser == null)
			//{
			//	Tool.WriteErrorLine("消息30037大鱼不存在");
			//	return false;
			//}
			//var bigFish = new SimpleUserData() { NickName = bigFishUser.GetNickName(), HeadIcon = bigFishUser.HeadIcon, UserId = bigFishUser.UserId };
			////TODO:土豪
			//var richMan = new SimpleUserData() { HeadIcon = "smallHead", NickName = "我是土豪" };

			//tableProfitRecord.MVP = mvp;
			//tableProfitRecord.BigFish = bigFish;
			//tableProfitRecord.RichMan = richMan;
			//result = true;
			//tablesStr = Convert.ToBase64String(ProtoBufUtils.Serialize(tableProfitRecord));
			return true;
		}

		public override void BuildPacket()
		{
			this.PushIntoStack(result);
			this.PushIntoStack(tablesStr);
		}
	}
}
